package com.dlyk.web;

import com.dlyk.model.Clue;
import com.dlyk.query.ClueQuery;
import com.dlyk.result.R;
import com.dlyk.service.ClueService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 线索管理
 */
@RestController
public class ClueController {

    @Resource
    private ClueService clueService;

    /**
     * 获取线索列表（PageHelper分页查询）
     */
    // 要想使用@PreAuthorize注解，需要在Spring Security配置类中使用@EnableMethodSecurity注解，开启方法级别的权限检查
    // 当前用户有 "clue:list" 这个权限标识符时才可以访问这个控制器
    @PreAuthorize(value = "hasAuthority('clue:list')")
    @GetMapping("/api/clue/list")
    public R clueListPage(@RequestParam(value = "current", required = false) Integer current) {

        // required = false  表示非必须，这个参数也可以不传，不传参数，默认查询第一页
        if (current == null) {
            current = 1;
        }

        PageInfo<Clue> cluePageInfo = clueService.getClueListByPage(current);

        return R.OK(cluePageInfo);
    }

    /**
     * 导入线索Excel
     *
     * 注意：文件参数的参数名要和前端提交的参数名保持一致
     */
    @PreAuthorize(value = "hasAuthority('clue:import')")
    @PostMapping("/api/clue/importExcel")
    public R importExcel(MultipartFile file) throws IOException {

        clueService.clueImportExcel(file);

        return R.OK();
    }

    /**
     * 加载下拉选项数据
     */
    @GetMapping("/api/clue/loadOptions")
    public R loadClueOptions() {

        Map<String, Object> options = clueService.getClueOptions();

        return R.OK(options);
    }

    /**
     * 验证手机号是否录入过（查询数据库中是否存在该手机号）
     */
    @GetMapping("/api/clue/checkPhone/{phone}")
    public R checkPhone(@PathVariable("phone") String phone) {

        int count = clueService.getCountByPhone(phone);

        // 返回值为0表示数据库中没有该数据，没有录入过，否则就是录入过
        return count == 0 ? R.OK() : R.FAIL();
    }

    /**
     * 录入线索
     */
    @PreAuthorize(value = "hasAuthority('clue:add')")
    @PostMapping("/api/clue/add")
    public R addClue(ClueQuery clueQuery) {

        int save = clueService.saveClue(clueQuery);

        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id查询线索
     */
    @GetMapping("/api/clue/get/{id}")
    public R getClueById(@PathVariable("id") Integer id) {

        Clue clue = clueService.getClueById(id);

        return R.OK(clue);
    }

    /**
     * 编辑线索
     */
    @PreAuthorize(value = "hasAuthority('clue:edit')")
    @PutMapping("/api/clue/edit")
    public R editClue(ClueQuery clueQuery) {

        int edit = clueService.editClue(clueQuery);

        return edit >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 获取线索详情
     */
    @PreAuthorize(value = "hasAuthority('clue:detail')")
    @GetMapping("/api/clue/detail/{id}")
    public R clueDetail(@PathVariable("id") Integer id) {

        Clue clue = clueService.getClueDetailById(id);

        return R.OK(clue);
    }

    /**
     * 删除线索
     */
    @PreAuthorize(value = "hasAuthority('clue:remove')")
    @DeleteMapping("/api/clue/remove/{id}")
    public R clueRemove(@PathVariable("id") Integer id) {

        int remove = clueService.removeClueById(id);

        return remove >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 批量删除线索
     */
    @PreAuthorize(value = "hasAuthority('clue:remove')")
    @DeleteMapping("/api/clue/batchRemove")
    public R clueBatchRemove(@RequestParam("ids") List<String> ids) {

        int batchRemove = clueService.batchRemoveClueByIds(ids);

        return batchRemove >= ids.size() ? R.OK() : R.FAIL();
    }

}

